package com.monitor.app_monitor.service.quartz.task;


import com.monitor.app_monitor.entity.ServiceList;
import com.monitor.app_monitor.entity.TaskLogAnnotation;
import com.monitor.app_monitor.service.MonitorService;
import com.monitor.app_monitor.service.quartz.utils.logUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


@DisallowConcurrentExecution //作业不并发
@Component
@Slf4j
public class ColectCheckJob implements Job{

    @Resource
    @Qualifier("appsMap")
    private Map<String, AtomicInteger> appsMap;

    @Autowired
    @Qualifier("urlAppMonitor")
    private MonitorService urlmonitorService;



    @Override
    @TaskLogAnnotation
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        /**
         * 采集应用程序存活状态监控
         */

        final JobDataMap jobDataMap = arg0.getJobDetail().getJobDataMap();


        ServiceList serviceList = new ServiceList();
        serviceList.setHost(String.valueOf(jobDataMap.get("host")));
        serviceList.setOwner(String.valueOf(jobDataMap.get("owner")));
        serviceList.setEmsName(String.valueOf(jobDataMap.get("emsName")));
        serviceList.setNetworkForm(String.valueOf(jobDataMap.get("networkForm")));
        serviceList.setEmsIp(String.valueOf(jobDataMap.get("emsIp")));
        serviceList.setGitAdds(String.valueOf(jobDataMap.get("gitAdds")));
        serviceList.setEnv(String.valueOf(jobDataMap.get("env")));
        serviceList.setPort((Integer) jobDataMap.get("port"));
        serviceList.setDeveloper(String.valueOf(jobDataMap.get("developer")));
        serviceList.setAppDescribe(String.valueOf(jobDataMap.get("appDescribe")));
        serviceList.setNote(String.valueOf(jobDataMap.get("note")));
        serviceList.setType(String.valueOf(jobDataMap.get("type")));
        serviceList.setId((Integer) jobDataMap.get("id"));

        int resCode = 0;
        resCode = urlmonitorService.receiveCode(serviceList);

        appsMap.get("appid-"+serviceList.getId()).set(resCode);

    }
}